package com.etc.library.service.impl;

import com.etc.library.dao.UserMapper;
import com.etc.library.pojo.User;
import com.etc.library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User logon(String username, String password) {
        return userMapper.logon(username, password);
    }

    @Transactional
    @Override
    public boolean register(User user) {
        if (!userMapper.checkName(user.getUsername())) {
            if (userMapper.add(user) > 0)
                return true;
            throw new RuntimeException("注册失败");
        }
        throw new RuntimeException("注册失败，用户名已经存在");
    }
}
